package com.qkl.xiatang.app.dto.device;


import com.qkl.xiatang.app.entity.device.DeviceNormalEntity;
import com.qkl.xiatang.app.utils.appenum.SensorTypeEnum;
import com.qkl.xiatang.app.utils.appenum.SensorTypeNormalEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class SensorDataDTO {

    private Long id;

    private String ts;

    private String sn;

    private String ph;
    private String phNormalValue = "";
    private boolean phNormal;
    //private DeviceNormalEntity phEntity;

    private String salinity;
    private String salinityNormalValue = "";
    private boolean salinityNormal;
    //private DeviceNormalEntity salinityEntity;

    private String waterTemp;
    private String waterTempNormalValue = "";
    private boolean waterTempNormal;
    //private DeviceNormalEntity waterTempEntity;

    private String do1;
    private String do1NormalValue = "";
    private boolean do1Normal;
    //private DeviceNormalEntity do1Entity;

    private String nh3No;
    private String nh3NoNormalValue = "";
    private boolean nh3NoNormal;
    //private DeviceNormalEntity nh3NoEntity;

    private String no2;
    private String no2NormalValue = "";
    private boolean no2Normal;
    //private DeviceNormalEntity no2Entity;

    /**
     * 感应项：1水温，2盐度，3 PH，4溶氧，5亚盐，6氨氮
     * @param sensorType
     * @return
     */
    public List<SensorDataListDTO> toList(String  sensorType){
        sensorType = StringUtils.trimToEmpty(sensorType);
        boolean emptyType = StringUtils.isEmpty(sensorType);
        List<SensorDataListDTO> list = new ArrayList<>();
        if(emptyType || this.inType(sensorType,1)){
            list.add(this.toWaterTemp());
        }
        if(emptyType || this.inType(sensorType,2)){
            list.add(this.toSalinity());
        }
        if(emptyType || this.inType(sensorType,3)){
            list.add(this.toPh());
        }
        if(emptyType || this.inType(sensorType,4)){
            list.add(this.toDo1());
        }
        if(emptyType || this.inType(sensorType,5)){
            list.add(this.toNo2());
        }
        if(emptyType || this.inType(sensorType,6)){
            list.add(this.toNh3No());
        }
        return list;
    }

    private boolean inType(String sensorType,int type){
        return sensorType.indexOf(String.valueOf(type)) > -1;
    }

    private SensorDataListDTO toPh(){
        return toMap(3,ph,phNormalValue,phNormal );
    }

    private SensorDataListDTO toSalinity(){
        return toMap(2,salinity,salinityNormalValue,salinityNormal );
    }

    private SensorDataListDTO toWaterTemp(){
        return toMap(1,waterTemp,waterTempNormalValue ,waterTempNormal);
    }

    private SensorDataListDTO toDo1(){
        return toMap(4,do1,do1NormalValue,do1Normal );
    }

    private SensorDataListDTO toNh3No(){
        return toMap(6,nh3No,nh3NoNormalValue,nh3NoNormal );
    }

    private SensorDataListDTO toNo2(){
        return toMap(5,no2,no2NormalValue ,no2Normal);
    }

    private SensorDataListDTO toMap(int type,String value,String normal,boolean isNormal){
        SensorDataListDTO dto = new SensorDataListDTO();
        dto.setType(type);
        dto.setTypeName(SensorTypeEnum.getTypeName(type));
        dto.setValue(value);
        dto.setNormal(normal);
        dto.setIsNormal(Boolean.valueOf(isNormal));

        SensorTypeNormalEnum normalEnum = SensorTypeEnum.getNormalEnum(type);
        DeviceNormalEntity entity = this.normalMap.get(normalEnum.getType());
        if(null != entity){
            dto.setValueMax(entity.getValueMax());
            dto.setValueMin(entity.getValueMin());
            dto.setNormalMin(entity.getNormalMin());
            dto.setNormalMax(entity.getNormalMax());

            /**
            if(null != entity.getNormalMin()){
                dto.setNormalMin(entity.getNormalMin());
            }else {
                dto.setNormalMin(dto.getValueMin());
            }

            if(null != entity.getNormalMax()){
                dto.setNormalMax(entity.getNormalMax());
            }else {
                dto.setNormalMax(dto.getValueMax());
            }
             **/
        }

        return dto;
    }

    private Map<Integer,DeviceNormalEntity> normalMap = new HashMap<>();

    public void setNormal(DeviceNormalEntity normalEntity){
        normalMap.put(normalEntity.getType(),normalEntity);
        Integer type = normalEntity.getType();//0 电池电压,1 pH值,2 盐度,3 水温,4 溶解氧,5 氨氮,6 亚硝酸盐
        String normalVal = normalEntity.normalValue();
        Double min = normalEntity.getNormalMin();
        Double max = normalEntity.getNormalMax();
        int iType = null == type? -1:type;
        if( 1 == iType){
            phNormalValue = normalVal;
            phNormal = isNormal(min,max,ph);
        }else if(2 == iType){
            salinityNormalValue  = normalVal;
            salinityNormal = isNormal(min,max,salinity);
        }else if(3 == iType){
            waterTempNormalValue  = normalVal;
            waterTempNormal = isNormal(min,max,waterTemp);
        }else if(4 == iType){
            do1NormalValue  = normalVal;
            do1Normal = isNormal(min,max,do1);
        }else if(5 == iType){
            nh3NoNormalValue  = normalVal;
            nh3NoNormal = isNormal(min,max,nh3No);
        }else if(6 == iType){
            no2NormalValue  = normalVal;
            no2Normal = isNormal(min,max,no2);
        }
    }

    private static boolean isNormal(Double min ,Double max,String valueStr){
        Double value = null;
        try{
            value = Double.valueOf(valueStr);
        }catch (Exception e){

        }
        return isNormal(min,max,value);
    }

    private static boolean isNormal(Double min ,Double max,Double value){
        if(null == min  && null != max){
            if(null == value || value > max){
                return false;
            }
        }
        if(null != min  && null == max){
            if(null == value || value < min){
                return false;
            }
        }
        if(null != min  && null != max){
            if(null == value || value < min || value > max){
                return false;
            }
        }
        return true;
    }
}
